/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author thien_000
 */
public class bean_chuyenbay implements Serializable {

    private String MaCB;
    private String NgayBay;
    private String MaNoiDi;
    private String MaNoiDen;

    public bean_chuyenbay() {

    }

    public bean_chuyenbay(String macb, String ngaybay, String manoidi, String manoiden) {
        this.MaCB = macb;
        this.MaNoiDen = manoiden;
        this.MaNoiDi = manoidi;
        this.NgayBay = ngaybay;
    }

    public bean_chuyenbay(String ngaybay, String manoidi, String manoiden) {
        this.MaNoiDen = manoiden;
        this.MaNoiDi = manoidi;
        this.NgayBay = ngaybay;
    }

    /**
     * @return the MaCB
     */
    public String getMaCB() {
        return MaCB;
    }

    /**
     * @return the NgayBay
     */
    public String getNgayBay() {
        return NgayBay;
    }

    /**
     * @return the MaNoiDi
     */
    public String getMaNoiDi() {
        return MaNoiDi;
    }

    /**
     * @return the MaNoiDen
     */
    public String getMaNoiDen() {
        return MaNoiDen;
    }

    /**
     * @param MaCB the MaCB to set
     */
    public void setMaCB(String MaCB) {
        this.MaCB = MaCB;
    }

    /**
     * @param NgayBay the NgayBay to set
     */
    public void setNgayBay(String NgayBay) {
        this.NgayBay = NgayBay;
    }

    /**
     * @param MaNoiDi the MaNoiDi to set
     */
    public void setMaNoiDi(String MaNoiDi) {
        this.MaNoiDi = MaNoiDi;
    }

    /**
     * @param MaNoiDen the MaNoiDen to set
     */
    public void setMaNoiDen(String MaNoiDen) {
        this.MaNoiDen = MaNoiDen;
    }

    public bean_chuyenbay[] hienthichuyenbay() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM chuyenbay";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();
            while (rs.next()) {
                String macb = rs.getString("MaCB");
                String ngaybay = rs.getString("NgayBay");
                String noidi = rs.getString("MaNoiDi");
                String noiden = rs.getString("MaNoiDen");
                bean_chuyenbay nv = new bean_chuyenbay(macb, ngaybay, noidi, noiden);
                list.add(nv);
            }
            bean_chuyenbay[] result = new bean_chuyenbay[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public bean_chuyenbay[] searchLikeMaCB(String MaCB) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM chuyenbay WHERE MaCB LIKE ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, "%" + MaCB + "%");
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String macb = rs.getString("MaCB");
                String ngaybay = rs.getString("NgayBay");
                String noidi = rs.getString("MaNoiDi");
                String noiden = rs.getString("MaNoiDen");
                bean_chuyenbay nv = new bean_chuyenbay(macb, ngaybay, noidi, noiden);
                list.add(nv);
            }
            bean_chuyenbay[] result = new bean_chuyenbay[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(String macb, String ngaybay, String noidi, String noiden) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "INSERT INTO chuyenbay(MaCB,NgayBay,MaNoiDi,MaNoiDen)" + "VALUES(?,?,?,?)";
            stm = con.prepareStatement(sql);
            stm.setString(1, macb);
            stm.setString(2, ngaybay);
            stm.setString(3, noidi);
            stm.setString(4, noiden);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteChuyenBay() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM chuyenbay WHERE MaCB=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, MaCB);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateChuyenBay() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "UPDATE chuyenbay SET NgayBay=?,MaNoiDi=?,MaNoiDen=? WHERE MaCB=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, NgayBay);
            stm.setString(2, MaNoiDi);
            stm.setString(3, MaNoiDen);
            stm.setString(4, MaCB);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteAll() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM chuyenbay";
            stm = con.prepareStatement(sql);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
